//调用createInnerAudioContext接口
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.loop = true;
let bgAudioMannager = ''
export default {
    data() {
        return {
            // 定时时间
            duration: 0,
            playWay: 1,
            playState: 1,
            nowIndex: 0,
            duration: 0,
            currentTime: 0,
            songList: [

            ],
            direction: '',
            tab: 0, //菜单栏控制 0自然音 1助眠音
            selected: 0, //助眠音专辑控制 0默认展示第一个专辑
            current: 0, //自然音控制 0默认第一个自然音
            array: [{
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/xiuxi.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/xiuxi.png",
                tit: '梦境唤醒',
                src: 'http://sleepclound.ltd:9000/temp/music/ShakingHigh%20-%20ECHO%EF%BC%88%E4%BA%94%E4%BA%BA%E5%94%B1%E8%A7%81%E5%8F%8C%E5%A3%B0%E9%81%93%EF%BC%8FShakingHigh%20Remix%EF%BC%89.mp3', // OSS 文件存储服务/系统
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/suisuibing.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/suisuibing.png",
                tit: '黑糖碎碎冰',
                src: 'http://music.163.com/song/media/outer/url?id=5265278.mp3'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/naizui.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/naizui.png",
                tit: '宝宝入睡Ⅰ',
                src: 'http://cdn.350800.xyz/music/2.mp3'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/jiabing.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/jiabing.png",
                tit: '奶昔加冰',
                src: 'http://cdn.350800.xyz/music/1.mp3'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/naizui.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/naizui.png",
                tit: '宝宝入睡Ⅱ',
                src: 'http://cdn.350800.xyz/music/3.mp3'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/piaoliuping.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/piaoliuping.png",
                tit: '漂流瓶',
                src: 'http://cdn.350800.xyz/music/4.mp3'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/haidao.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/haidao.png",
                tit: '海鸟嬉戏',
                src: 'http://cdn.350800.xyz/music/5.mp3'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/xiangbin.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/xiangbin.png",
                tit: '起泡香槟',
                src: 'http://cdn.350800.xyz/music/6.mp3'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/yudi.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>", //自然音图片地址
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/yudi.png",
                tit: '初春小雨' //自然音标题
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/chunyu.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/chunyu.png",
                tit: '春雨如油'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/leiyu.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/leiyu.png",
                tit: '夏雷雨落'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/chengsan.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/chengsan.png",
                tit: '雨中撑伞'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/yudi.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/yudi.png",
                tit: '雨滴叶落'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/kele.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/kele.png",
                tit: '冰镇可乐'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/pingdiguo.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/pingdiguo.png",
                tit: '打蛋下锅'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/yemu.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/yemu.png",
                tit: '夜幕降临'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/guo.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/guo.png",
                tit: '大火收汁'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/gangqin.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/gangqin.png",
                tit: '神奇时刻Ⅱ'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/jiubei.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/jiubei.png",
                tit: '薄荷鸡尾酒'
            }, {
                img: "<image src='http://sleepclound.ltd:9000/temp/sleed/bingkuai.png' mode='scaleToFill' style='width: 100%;height: 100%;'></image>",
                imgUrl: "http://sleepclound.ltd:9000/temp/sleed/bingkuai.png",
                tit: '清脆咬冰'
            }],
            // 助眠音
            imgArr: [{
                url: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp5.gexing.com%2FGSF%2Ftouxiang%2F20200726%2F15%2F2h5mi4mareytqw8kaw2vfol2s.jpg%40%21200x200_3%3Frecache%3D20131108&refer=http%3A%2F%2Fp5.gexing.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621686662&t=d2406c65225ac4defc481912e3a7e9f7", //专辑图片地址
                title: "星愿钢琴曲", //专辑名称
                author: "HIFIVE MUSIC", //专辑作者
                introduction: "介绍： 按下喧嚣的静音键让音乐带你进入平静安稳的状态睡眠伴侣 伴你整夜好眠(更多助眠背景声 请进入官方歌单 助眠系列）", //专辑简介
                count: 3, //助眠音列表数
                collect: 1, //收藏状态 0未收藏 1已收藏
                musicArr: [{
                    name: "晨风", //歌曲名
                    time: "01:54", //歌曲时长
                    album: "HIFIVE MUSIC", //所属专辑
                    author: "蜗牛睡眠", //作者
                    src: 'http://sleepclound.ltd:9000/temp/music/ShakingHigh%20-%20ECHO%EF%BC%88%E4%BA%94%E4%BA%BA%E5%94%B1%E8%A7%81%E5%8F%8C%E5%A3%B0%E9%81%93%EF%BC%8FShakingHigh%20Remix%EF%BC%89.mp3', // OSS 文件存储服务/系统
                    collect:'1'
                }, {
                    name: "春夜喜雨",
                    time: "02:36",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=866018207.mp3',
                    collect:'1'
                }, {
                    name: "冬",
                    time: "03:30",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=430685718.mp3',
                }, ]
            }, {
                url: "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3988951758,1550689842&fm=15&gp=0.jpg",
                title: "星愿钢琴曲1",
                author: "HIFIVE MUSIC1",
                introduction: "1介绍： 按下喧嚣的静音键让音乐带你进入平静安稳的状态睡眠伴侣 伴你整夜好眠(更多助眠背景声 请进入官方歌单 助眠系列）",
                count: 3,
                collect: 0,
                musicArr: [{
                    name: "春夜喜雨",
                    time: "02:36",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=866018207.mp3',
                }, {
                    name: "冬",
                    time: "03:30",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=866018207.mp3',
                }, {
                    name: "宁静时光",
                    time: "05:14",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=866018207.mp3',
                }]
            }, {
                url: "http://www.webjx.com/files/allimg/120907/0200593.jpg",
                title: "星愿钢琴曲2",
                author: "HIFIVE MUSIC2",
                introduction: "2介绍： 按下喧嚣的静音键让音乐带你进入平静安稳的状态睡眠伴侣 伴你整夜好眠(更多助眠背景声 请进入官方歌单 助眠系列）",
                count: 3,
                collect: 0,
                musicArr: [{
                    name: "晨风",
                    time: "01:54",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=866018207.mp3',
                }, {
                    name: "冬",
                    time: "03:30",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=866018207.mp3',
                }, {
                    name: "宁静时光",
                    time: "05:14",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=866018207.mp3',

                }]
            }, {
                url: "http://p3.music.126.net/qWkwJrQrvkXd1URVoAKX8A==/109951164520447993.jpg?param=200y200",
                title: "星愿钢琴曲3",
                author: "HIFIVE MUSIC3",
                introduction: "3介绍： 按下喧嚣的静音键让音乐带你进入平静安稳的状态睡眠伴侣 伴你整夜好眠(更多助眠背景声 请进入官方歌单 助眠系列）",
                count: 3,
                collect: 0,
                musicArr: [{
                    name: "晨风",
                    time: "01:54",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=866018207.mp3',
                }, {
                    name: "春夜喜雨",
                    time: "02:36",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=866018207.mp3',
                }, {
                    name: "宁静时光",
                    time: "05:14",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=866018207.mp3',
                }]
            }, {
                url: "http://p3.music.126.net/ck9MN9zod6hqw5vSuaHPTw==/109951164362865822.jpg?param=200y200",
                title: "星愿钢琴曲4",
                author: "HIFIVE MUSIC4",
                introduction: "4介绍： 按下喧嚣的静音键让音乐带你进入平静安稳的状态睡眠伴侣 伴你整夜好眠(更多助眠背景声 请进入官方歌单 助眠系列）",
                count: 3,
                collect: 0,
                musicArr: [{
                    name: "晨风",
                    time: "01:54",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=866018207.mp3',
                }, {
                    name: "春夜喜雨",
                    time: "02:36",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=866018207.mp3',
                }, {
                    name: "冬",
                    time: "03:30",
                    album: "HIFIVE MUSIC",
                    author: "蜗牛睡眠",
                    src: 'http://music.163.com/song/media/outer/url?id=866018207.mp3',
                }]
            }],
            swiperList: [{
                id: 0,
                type: 'image',
                url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big84000.jpg'
            }, {
                id: 1,
                type: 'image',
                url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big37006.jpg',
            }, {
                id: 2,
                type: 'image',
                url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big39000.jpg'
            }, {
                id: 3,
                type: 'image',
                url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big10001.jpg'
            }, {
                id: 4,
                type: 'image',
                url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big25011.jpg'
            }, {
                id: 5,
                type: 'image',
                url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big21016.jpg'
            }, {
                id: 6,
                type: 'image',
                url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big99008.jpg'
            }],
            doPlayMusic: {}
        }
    },
    onLoad: function() {
        this.TowerSwiper('swiperList');
        bgAudioMannager = uni.getBackgroundAudioManager();
    },
    computed: {
        'nowtime': function() {
            var that = this
            var s = that.currentTime
            //计算分钟
            //算法：将秒数除以60，然后下舍入，既得到分钟数
            var h;
            h = Math.floor(s / 60);
            //计算秒
            //算法：取得秒%60的余数，既得到秒数
            s = s % 60;
            //将变量转换为字符串
            h += '';
            s += '';
            //如果只有一位数，前面增加一个0
            h = (h.length == 1) ? '0' + h : h;
            s = (s.length == 1) ? '0' + s : s;
            return h + ':' + s;
        },
        'alltime': function() {
            var that = this
            var s = that.duration
            //计算分钟
            //算法：将秒数除以60，然后下舍入，既得到分钟数
            var h;
            h = Math.floor(s / 60);
            //计算秒
            //算法：取得秒%60的余数，既得到秒数
            s = s % 60;
            //将变量转换为字符串
            h += '';
            s += '';
            //如果只有一位数，前面增加一个0
            h = (h.length == 1) ? '0' + h : h;
            s = (s.length == 1) ? '0' + s : s;
            return h + ':' + s;
        }
    },
    methods: {
        sliderChange(e) {
            this.currentTime = e.detail.value
            bgAudioMannager.seek(this.currentTime)
        },
        bgAudioInnit() {
            var that = this
            bgAudioMannager.title = that.songList[that.nowIndex].name;
            bgAudioMannager.singer = that.songList[that.nowIndex].author;
            bgAudioMannager.coverImgUrl = that.songList[that.nowIndex].img;
            bgAudioMannager.src = that.songList[that.nowIndex].src;

            bgAudioMannager.onPlay(() => {
                that.playFunc()
            })
            bgAudioMannager.onPause(() => {
                that.pauseFunc()
            })
            bgAudioMannager.onEnded(() => {
                that.next()
            })
            bgAudioMannager.onTimeUpdate(() => {
                this.currentTime = Math.floor(bgAudioMannager.currentTime);
            })
            bgAudioMannager.onPrev(function() {
                that.last()
            })
            bgAudioMannager.onNext(function() {
                that.next()
            })

            bgAudioMannager.onError(function() {
                that.error()
            })
            bgAudioMannager.onWaiting(function() {
                that.playState = 0
            })

            bgAudioMannager.onCanplay(function() {
                that.duration = Math.floor(bgAudioMannager.duration)
            })

        },
        error() {
            this.playState = 0
        },
        play() {
            var that = this
            if (bgAudioMannager.src == undefined) {
                this.bgAudioInnit()
            }
            bgAudioMannager.play()

        },
        pause() {
            var that = this

            bgAudioMannager.pause()
        },
        playFunc() {
            var that = this
            that.playState = 0
        },
        pauseFunc() {
            var that = this
            that.playState = 1
        },
        last() {
            var that = this
            //随机
            if (that.playWay == 2) {
                that.nowIndex = Math.floor(Math.random() * this.songList.length);
            } else if (that.playWay == 1) { //顺序播放
                //顺序
                if (that.nowIndex < 1) {
                    that.nowIndex = that.songList.length - 1
                } else {
                    that.nowIndex = that.nowIndex - 1
                }
            } else { //单曲播放 不做处理

            }

            that.bgAudioInnit()
        },
        next() {
            var that = this
            //随机
            if (that.playWay == 2) {
                that.nowIndex = Math.floor(Math.random() * this.songList.length);
            } else if (that.playWay == 1) { //顺序播放
                //顺序
                if (that.nowIndex >= (this.songList.length - 1)) {
                    that.nowIndex = 0
                } else {
                    that.nowIndex = that.nowIndex + 1
                }
            } else { //单曲播放 不做处理

            }
            that.bgAudioInnit()
        },
        updateWay() {
            var that = this
            if (that.playWay == 2) {
                that.playWay = 0
            } else {
                that.playWay = that.playWay + 1
            }

        },
        // towerSwiper
        // 初始化towerSwiper 轮播图
        TowerSwiper(name) {
            let list = this[name];
            for (let i = 0; i < list.length; i++) {
                // parseInt转化成整数如3/2=1.5转化成整数1
                // abs绝对值
                // zIndex：高度
                list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
                // mLeft：左边
                list[i].mLeft = i - parseInt(list.length / 2)
            }
            // swiperList：数组
            this.swiperList = list
        },

        // towerSwiper触摸开始
        TowerStart(e) {
            this.towerStart = e.touches[0].pageX
        },

        // towerSwiper计算方向
        TowerMove(e) {
            this.direction = e.touches[0].pageX - this.towerStart > 0 ? 'right' : 'left'
        },

        // towerSwiper计算滚动
        TowerEnd(e) {
            let direction = this.direction;
            let list = this.swiperList;
            if (direction == 'right') {
                let mLeft = list[0].mLeft;
                let zIndex = list[0].zIndex;
                for (let i = 1; i < this.swiperList.length; i++) {
                    this.swiperList[i - 1].mLeft = this.swiperList[i].mLeft
                    this.swiperList[i - 1].zIndex = this.swiperList[i].zIndex
                }
                this.swiperList[list.length - 1].mLeft = mLeft;
                this.swiperList[list.length - 1].zIndex = zIndex;
            } else {
                let mLeft = list[list.length - 1].mLeft;
                let zIndex = list[list.length - 1].zIndex;
                for (let i = this.swiperList.length - 1; i > 0; i--) {
                    this.swiperList[i].mLeft = this.swiperList[i - 1].mLeft
                    this.swiperList[i].zIndex = this.swiperList[i - 1].zIndex
                }
                this.swiperList[0].mLeft = mLeft;
                this.swiperList[0].zIndex = zIndex;
            }
            this.direction = ""
            this.swiperList = this.swiperList
        },
        playmusic(obj, i, item) {
            console.log("当前播放obj: ", obj);
            console.log("当前播放index: ", i);
            console.log("当前播放item: ", item);

            var that = this

            obj = that.imgArr[that.selected].musicArr[i]
            console.log("重新计算后的obj: ", obj);
            bgAudioMannager.title = obj.name;
            bgAudioMannager.singer = obj.author;
            // bgAudioMannager.coverImgUrl = obj.img;
            bgAudioMannager.src = obj.src;
            that.doPlayMusic = obj;
            // this.
            this.play();
        },

        //封装audio方法
        audio(e) {
            /* 
            innerAudioContext.autoplay = true;			innerAudioContext.src = e;			innerAudioContext.onPlay(() => {				console.log('开始播放');			});			innerAudioContext.onError((res) => {				console.log(res.errMsg);				console.log(res.errCode);			}); 
            */
            // 音乐播放的暂停和开始
            let _this = this
            console.log(_this.music + "1")
            if (_this.music == e) {
                if (innerAudioContext.paused) {
                    innerAudioContext.play()
                } else {
                    innerAudioContext.pause();
                    innerAudioContext.onPause(() => {
                        innerAudioContext.startTime = innerAudioContext.currentTime
                    })
                }
            } else {
                innerAudioContext.src = e
                if (innerAudioContext.paused) {
                    innerAudioContext.play()
                } else {
                    innerAudioContext.pause();
                    innerAudioContext.onPause(() => {
                        innerAudioContext.startTime = innerAudioContext.currentTime
                    })
                }
            }

            console.log(_this.music + "2")
            _this.music = e

            console.log(_this.music + "3")
        },
        //scroll:页面的滚轴
        scroll: function(e) {
            console.log(e);
            this.old.scrollTop = e.detail.scrollTop
        },
        shoucang(musicArr) {
            uni.setStorageSync('shoucang', musicArr)
            uni.request({
                data: musicArr,
                method: 'POST',
                url: 'http://sleepclound.ltd:8087/auth/temp_login'
            })
            console.log(musicArr)

        },

        choseTime() {
            const that = this;
            let durationList = [0, 1, 10, 30]
            let index = durationList.indexOf(that.duration);
            // 超出则回到0
            if (index + 1 > durationList.length - 1) {
                that.duration = durationList[0]
            } else {
                that.duration = durationList[index + 1]
            }

            let timeOutId;
            if (that.duration == 0) {
                uni.showToast({
                    title: "已关闭定时",
                    icon: 'none'
                })
                that.clearTimeout(timeOutId);

            } else {
                uni.showToast({
                    title: `将在${that.duration}分钟后停止播放`,
                    icon: 'none'
                })

                // 先重置定时器，再定时
                that.clearTimeout(timeOutId);
                
                timeOutId = setTimeout(function() {
                    innerAudioContext.stop();
                    uni.showToast({
                        title: `定时时间到，已停止播放`,
                        icon: 'none'
                    })
                    
                    // 重置定时时间
                    that.duration=0
                }, that.duration * 1000 * 60)
            }
        },
        // 清除定时器
        clearTimeout(timeOutId) {
            if (timeOutId) {
                clearTimeout(timeOutId)
            }
        }
    }
}
